#include "CDiceCfg.h"
#include "../item/CDropItem.h"

int CFamilyDiceConfig::Init()
{
    _astDiceGridConfigs.m_iUsedCount = MAX_DICE_GRID_NUMS+1;
    _astDicePayConfigs.m_iUsedCount = MAX_DICE_PAY_NUMS+1;
    _astDiceLevelConfigs.m_iUsedCount = MAX_DICE_LEVEL_NUMS+1;

    for (int i = 0; i < _astDiceGridConfigs.Size(); ++i)
    {
        _astDiceGridConfigs[i].Clear();
    }

    for (int i = 0; i < _astDicePayConfigs.Size(); ++i)
    {
        _astDicePayConfigs[i].Clear();
    }

    for (int i = 0; i < _astDiceLevelConfigs.Size(); ++i)
    {
        _astDiceLevelConfigs[i].Clear();
    }

    return 0;
}

int CFamilyDiceConfig::AddDiceGridConfig( CDiceAwardConfigData& stData )
{
    if (!stData.HasConfig())
    {
        return ERROR_DICE_GRID_ADD_EMPTY_CONFIG;
    }

    if( stData.iGridId>=_astDiceGridConfigs.GetCount() )
    {
        return ERROR_DICE_GRID_ID;
    }

    if (_astDiceGridConfigs[stData.iGridId].HasConfig())
    {
        return ERROR_DICE_GRID_ID;
    }

    _astDiceGridConfigs[stData.iGridId] = stData;
    _iMaxGridID = CT_MAX(stData.iGridId, _iMaxGridID);

    return 0;
}

int CFamilyDiceConfig::GetDiceGridConfig( short iGridId, CDiceAwardConfigData& stData )
{
    if( iGridId>= _astDiceGridConfigs.GetCount() )
    {
        return ERROR_DICE_GRID_ID;
    }

    if( !_astDiceGridConfigs[iGridId]._bHasConfig )
    {
        return ERROR_DICE_GRID_ID;
    }

    stData = _astDiceGridConfigs[iGridId];

    return 0;
}

int CFamilyDiceConfig::AddPayDiceConfig( CPayDiceConfigData& stData)
{
    if (!stData.HasConfig())
    {
        return ERROR_DICE_PAY_ADD_EMPTY_CONFIG;
    }

    if( stData.iTimes>=_astDicePayConfigs.GetCount() )
    {
        return ERROR_DICE_PAY_TIMES;
    }

    if (_astDicePayConfigs[stData.iTimes].HasConfig())
    {
        return ERROR_DICE_PAY_TIMES;
    }

    _astDicePayConfigs[stData.iTimes] = stData;

    return 0;
}

int CFamilyDiceConfig::GetPayDiceConfig( short iTimes, CPayDiceConfigData& stData)
{
    if( iTimes >= _astDicePayConfigs.GetCount() )
    {
        return ERROR_DICE_PAY_TIMES;
    }

    if( !_astDicePayConfigs[iTimes]._bHasConfig )
    {
        return ERROR_DICE_PAY_TIMES;
    }

    stData = _astDicePayConfigs[iTimes];

    return 0;
}


int CFamilyDiceConfig::AddLevelAwardConfig( short iLayer, TAward& stData )
{
    if( iLayer>_astDiceLevelConfigs.GetCount() )
    {
        return ERROR_DICE_LEVEL;
    }

    if (_astDiceLevelConfigs[iLayer].HasConfig())
    {
        _astDiceLevelConfigs[iLayer]._aTAwards.AddOneItem(stData);
    }
    else
    {
        _astDiceLevelConfigs[iLayer]._aTAwards.AddOneItem(stData);
        _astDiceLevelConfigs[iLayer]._bHasConfig = true;
    }

    return 0;
}

int CFamilyDiceConfig::GetLevelAwardConfig( short iLevel, CDiceLevelAwardConfigData& stData )
{
    if( iLevel>_astDiceLevelConfigs.GetCount() )
    {
        return ERROR_DICE_LEVEL;
    }

    if( !_astDiceLevelConfigs[iLevel]._bHasConfig )
    {
        return ERROR_DICE_LEVEL;
    }

    stData = _astDiceLevelConfigs[iLevel];

    return 0;
}



int CFamilyDiceConfig::AddAwardRateConfig( CDiceAwardRateData& stData )
{
    if (!stData.HasConfig())
    {
        return ERROR_DICE_AWARD_RATE_ADD_EMPTY_CONFIG;
    }

    if( stData.iDiceLevel>_astDiceAwardRateConfigs.GetCount() )
    {
        return ERROR_DICE_LEVEL_AWARD_RATE;
    }

    if (_astDiceAwardRateConfigs[stData.iDiceLevel].HasConfig())
    {
        return ERROR_DICE_LEVEL_AWARD_RATE;
    }

    _astDiceAwardRateConfigs[stData.iDiceLevel] = stData;

    return 0;
}

int CFamilyDiceConfig::GetAwardRateConfig( short iDiceLevel, CDiceAwardRateData& stData )
{
    if( iDiceLevel>_astDiceAwardRateConfigs.GetCount() )
    {
        return ERROR_DICE_LEVEL_AWARD_RATE;
    }

    if( !_astDiceAwardRateConfigs[iDiceLevel]._bHasConfig )
    {
        return ERROR_DICE_LEVEL_AWARD_RATE;
    }

    stData = _astDiceAwardRateConfigs[iDiceLevel];

    return 0;
}

int CFamilyDiceConfig::AddRankConfig(int iRank, CDropItemData & stData )
{
    if (iRank <= 0 || iRank > MAX_DICE_RANK_SIZE)
    {
        CT_ERROR(("%d", iRank));
        return ERROR_DICE_RANK_AWARD_CONFIG;
    }

    if (_astDiceRankAward[iRank-1]._ucItemType > 0)
    {
        CT_ERROR(("%d", iRank));
        return ERROR_DICE_RANK_AWARD_CONFIG;
    }

    _astDiceRankAward[iRank-1] = stData;
    _astDiceRankAward.m_iUsedCount += 1;
    return 0;
}

int CFamilyDiceConfig::GetRankConfig(int iRank, CDropItemData & stData )
{
    if (iRank <= 0 || iRank > MAX_DICE_RANK_SIZE)
    {
        CT_ERROR(("%d", iRank));
        return ERROR_DICE_RANK_AWARD_CONFIG;
    }    

    stData = _astDiceRankAward[iRank-1];
    return 0;
}
